package cn.ckx.coding.common.dto;

import cn.ckx.coding.common.exception.ErrorCodes;

import java.io.Serializable;

/**
 * http response object
 * @author kunxin.cai
 */
public class ApiResponse<T> implements Serializable {

    private static final long serialVersionUID = 1L;

    private static final String ENABLED_SUCCESS_MSG = "success";
    
    private static final String ENABLED_ERROR_MSG = "error";

    private Integer code;

    private String msg;

    private T data;
    
    private Long timestamp;

    private ApiResponse() { 
    }

    private ApiResponse(int code, String msg) {
        this.code  = code;
        this.msg = msg;
        this.timestamp = System.currentTimeMillis();
    }

    public static <T> ApiResponse<T> ok() {
        return new ApiResponse<>(ErrorCodes.SUCCESS,ENABLED_SUCCESS_MSG);
    }
    
    public  static <T> ApiResponse<T> ok(String msg) {
        return new ApiResponse<>(ErrorCodes.SUCCESS,msg);
    }

    public static <T> ApiResponse<T> error(int code) {
        return new ApiResponse<>(code,ENABLED_ERROR_MSG);
    }
    
    public static <T> ApiResponse<T> error(int code, String msg) {
        return new ApiResponse<>(code,msg);
    }
    
    public ApiResponse<T> of(T data){
        this.data = data;
        return this;
    } 

    public Integer getCode() {
        return code;
    }

    public String getMsg() {
        return msg;
    }

    public Long getTimestamp() {
        return timestamp;
    }

    public T getData() {
        return data;
    }
}

